﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace FileRenameTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string[] dosya;

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowNewFolderButton = false;
            folderBrowserDialog1.ShowDialog();
            listBox1.Items.Clear();

            try
            {
                dosya = Directory.GetFiles(folderBrowserDialog1.SelectedPath, textBox1.Text);
            }
            catch (Exception Hata)
            {
                MessageBox.Show(Hata.Message, "Run Time Select Error!");
                return;
            }

            for (Int32 i = 0; i < dosya.Length; i++)
            {
                FileInfo fi = new FileInfo(dosya[i]);
                listBox1.Items.Add(fi.Name);
            }
            textBox2.Text = folderBrowserDialog1.SelectedPath;
            button2.Enabled = true;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            string[] dosyalar;
            dosyalar=new string[listBox1.Items.Count];  //Bu iki satırda listbox1 de gösterilen dosya isimleri için bir dizideğişken tanımlanıyor.

            for (Int32 x = 0; x < listBox1.Items.Count; x++)
            {
                //MessageBox.Show(listBox1.Items[x].ToString());
                dosyalar[x] = listBox1.Items[x].ToString();

                if (checkBox1.Checked && checkBox1.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox3.Text ,textBox4.Text);
                }

                if (checkBox2.Checked && checkBox2.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox5.Text, textBox6.Text);
                }

                if (checkBox3.Checked && checkBox3.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox7.Text, textBox8.Text);
                }

                if (checkBox4.Checked && checkBox4.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox9.Text, textBox10.Text);
                }

                if (checkBox5.Checked && checkBox5.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox11.Text, textBox12.Text);
                }

                if (checkBox6.Checked && checkBox6.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox13.Text, textBox14.Text);
                }

                if (checkBox7.Checked && checkBox7.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox15.Text, textBox16.Text);
                }

                if (checkBox8.Checked && checkBox8.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox17.Text, textBox18.Text);
                }

                if (checkBox9.Checked && checkBox9.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox19.Text, textBox20.Text);
                }

                if (checkBox10.Checked && checkBox10.Enabled)
                {
                    dosyalar[x] = dosyalar[x].ToString().Replace(textBox21.Text, textBox22.Text);
                }
                listBox2.Items.Add(dosyalar[x]);
                button3.Enabled = true;
            }

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text == "" | textBox4.Text == "")
            {
                checkBox1.Enabled = false;
            }
            else
            {
                checkBox1.Enabled = true;
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            textBox3_TextChanged(sender,e);
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (textBox5.Text == "" | textBox6.Text == "")
            {
                checkBox2.Enabled = false;
            }
            else
            {
                checkBox2.Enabled = true;
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            textBox5_TextChanged(sender, e);
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            if (textBox7.Text == "" | textBox8.Text == "")
            {
                checkBox3.Enabled = false;
            }
            else
            {
                checkBox3.Enabled = true;
            }
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            textBox7_TextChanged(sender, e);
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            if (textBox9.Text == "" | textBox10.Text == "")
            {
                checkBox4.Enabled = false;
            }
            else
            {
                checkBox4.Enabled = true;
            }
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            textBox9_TextChanged(sender, e);
        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            if (textBox11.Text == "" | textBox12.Text == "")
            {
                checkBox5.Enabled = false;
            }
            else
            {
                checkBox5.Enabled = true;
            }
        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {
            textBox11_TextChanged(sender, e);
        }

        private void textBox13_TextChanged(object sender, EventArgs e)
        {
            if (textBox13.Text == "" | textBox14.Text == "")
            {
                checkBox6.Enabled = false;
            }
            else
            {
                checkBox6.Enabled = true;
            }
        }

        private void textBox14_TextChanged(object sender, EventArgs e)
        {
            textBox13_TextChanged(sender, e);
        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {
            if (textBox15.Text == "" | textBox16.Text == "")
            {
                checkBox7.Enabled = false;
            }
            else
            {
                checkBox7.Enabled = true;
            }
        }

        private void textBox16_TextChanged(object sender, EventArgs e)
        {
            textBox15_TextChanged(sender, e);
        }

        private void textBox17_TextChanged(object sender, EventArgs e)
        {
            if (textBox17.Text == "" | textBox18.Text == "")
            {
                checkBox8.Enabled = false;
            }
            else
            {
                checkBox8.Enabled = true;
            }
        }

        private void textBox18_TextChanged(object sender, EventArgs e)
        {
            textBox17_TextChanged(sender, e);
        }

        private void textBox19_TextChanged(object sender, EventArgs e)
        {
            if (textBox19.Text == "" | textBox20.Text == "")
            {
                checkBox9.Enabled = false;
            }
            else
            {
                checkBox9.Enabled = true;
            }
        }

        private void textBox20_TextChanged(object sender, EventArgs e)
        {
            textBox19_TextChanged(sender, e);
        }

        private void textBox21_TextChanged(object sender, EventArgs e)
        {
            if (textBox21.Text == "" | textBox22.Text == "")
            {
                checkBox10.Enabled = false;
            }
            else
            {
                checkBox10.Enabled = true;
            }
        }

        private void textBox22_TextChanged(object sender, EventArgs e)
        {
            textBox21_TextChanged(sender, e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Reset();
            folderBrowserDialog1.ShowNewFolderButton = true;
            folderBrowserDialog1.ShowDialog();

            if (folderBrowserDialog1.SelectedPath == "")
            {
                MessageBox.Show("Please Select A Folder!", "Run Titime Copy Error!");
                return;
            }

            for (Int32 x = 0; x < listBox1.Items.Count; x++)
            {
                FileInfo kopya = new FileInfo(textBox2.Text + "\\" +Convert.ToString(listBox1.Items[x]));
                kopya.CopyTo(folderBrowserDialog1.SelectedPath + "\\" + Convert.ToString(listBox2.Items[x]),true);
            }
            MessageBox.Show("All Files Copyed to " + folderBrowserDialog1.SelectedPath,"Copy Process Successful");

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            XmlTextWriter config = new XmlTextWriter("frt_config.xml", System.Text.Encoding.GetEncoding("windows-1254"));
            config.Formatting = Formatting.Indented;
            config.WriteStartDocument();
            config.WriteComment("File Rename Tool Config XML");

            config.WriteStartElement("Config");

            config.WriteStartElement("Mask");
            config.WriteString(textBox1.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check1");
            config.WriteString(checkBox1.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool1a");
            config.WriteString(textBox3.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool1b");
            config.WriteString(textBox4.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check2");
            config.WriteString(checkBox2.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool2a");
            config.WriteString(textBox5.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool2b");
            config.WriteString(textBox6.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check3");
            config.WriteString(checkBox3.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool3a");
            config.WriteString(textBox7.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool3b");
            config.WriteString(textBox8.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check4");
            config.WriteString(checkBox4.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool4a");
            config.WriteString(textBox9.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool4b");
            config.WriteString(textBox10.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check5");
            config.WriteString(checkBox5.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool5a");
            config.WriteString(textBox11.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool5b");
            config.WriteString(textBox12.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check6");
            config.WriteString(checkBox6.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool6a");
            config.WriteString(textBox13.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool6b");
            config.WriteString(textBox14.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check7");
            config.WriteString(checkBox7.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool7a");
            config.WriteString(textBox15.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool7b");
            config.WriteString(textBox16.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check8");
            config.WriteString(checkBox8.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool8a");
            config.WriteString(textBox17.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool8b");
            config.WriteString(textBox18.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check9");
            config.WriteString(checkBox9.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool9a");
            config.WriteString(textBox19.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool9b");
            config.WriteString(textBox20.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("check10");
            config.WriteString(checkBox10.Checked.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool10a");
            config.WriteString(textBox21.Text.ToString());
            config.WriteEndElement();
            config.WriteStartElement("Rool10b");
            config.WriteString(textBox22.Text.ToString());
            config.WriteEndElement();

            config.WriteEndElement();
            config.WriteEndDocument();
            config.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FileInfo varmi = new FileInfo("frt_config.xml");
            if (varmi.Exists)
            {
                XmlTextReader config = new XmlTextReader("frt_config.xml");
                while (config.Read())
                {
                    if (config.NodeType == XmlNodeType.Element)
                    {
                        if (config.Name == "Mask")
                        {
                            textBox1.Text = config.ReadString();
                        }
                        if (config.Name == "check1")
                        {
                            checkBox1.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool1a")
                        {
                            textBox3.Text = config.ReadString();
                        }
                        if (config.Name == "Rool1b")
                        {
                            textBox4.Text = config.ReadString();
                        }
                        if (config.Name == "check2")
                        {
                            checkBox2.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool2a")
                        {
                            textBox5.Text = config.ReadString();
                        }
                        if (config.Name == "Rool2b")
                        {
                            textBox6.Text = config.ReadString();
                        }
                        if (config.Name == "check3")
                        {
                            checkBox3.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool3a")
                        {
                            textBox7.Text = config.ReadString();
                        }
                        if (config.Name == "Rool3b")
                        {
                            textBox8.Text = config.ReadString();
                        }
                        if (config.Name == "check4")
                        {
                            checkBox4.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool4a")
                        {
                            textBox9.Text = config.ReadString();
                        }
                        if (config.Name == "Rool4b")
                        {
                            textBox10.Text = config.ReadString();
                        }
                        if (config.Name == "check5")
                        {
                            checkBox5.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool5a")
                        {
                            textBox11.Text = config.ReadString();
                        }
                        if (config.Name == "Rool5b")
                        {
                            textBox12.Text = config.ReadString();
                        }
                        if (config.Name == "check6")
                        {
                            checkBox6.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool6a")
                        {
                            textBox13.Text = config.ReadString();
                        }
                        if (config.Name == "Rool6b")
                        {
                            textBox14.Text = config.ReadString();
                        }
                        if (config.Name == "check7")
                        {
                            checkBox7.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool7a")
                        {
                            textBox15.Text = config.ReadString();
                        }
                        if (config.Name == "Rool7b")
                        {
                            textBox16.Text = config.ReadString();
                        }
                        if (config.Name == "check8")
                        {
                            checkBox8.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool8a")
                        {
                            textBox17.Text = config.ReadString();
                        }
                        if (config.Name == "Rool8b")
                        {
                            textBox18.Text = config.ReadString();
                        }
                        if (config.Name == "check9")
                        {
                            checkBox9.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool9a")
                        {
                            textBox19.Text = config.ReadString();
                        }
                        if (config.Name == "Rool9b")
                        {
                            textBox20.Text = config.ReadString();
                        }
                        if (config.Name == "check10")
                        {
                            checkBox10.Checked = Convert.ToBoolean(config.ReadString());
                        }
                        if (config.Name == "Rool10a")
                        {
                            textBox21.Text = config.ReadString();
                        }
                        if (config.Name == "Rool10b")
                        {
                            textBox22.Text = config.ReadString();
                        }
                    }
                }
                config.Close();

            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form Aboutbox = new AboutBox1();
            Aboutbox.ShowDialog();
        }

    }
}
